var fs = require("fs");
var cheerio = require("cheerio");
var child_process = require("child_process");

var username = "yijianlianblog";
var realname = "易建联";
var baseurl = "http://weibo.cn/" + username;
var count = 51;

var file = fs.createWriteStream(realname + ".csv", {flags: "a"});

function main(i) {
	if (i > count) {
		return;
	}
	
	var py = child_process.spawn("python", ["bee.py", baseurl + "?page=" + i]);
	
	var timeout_id = setTimeout(function() {
		py.kill();
		main(i);
	}, 10000);

	var s = "";
	py.stdout.on("data", function(d) {
		s += d.toString();
	});

	py.on("exit", function(code, signal) {
		if (signal == null) {
			clearTimeout(timeout_id);
			parse(s, i);
		}
	});
}

function parse(s, global_index) {
	console.log(global_index);
	var $ = cheerio.load(s);
	$(".c").each(function(i, elem) {
		if ($(this).attr("id") == null) {
			return;
		}
		var tweet = {
			content: "",
			date: "",
			fav: 0,
			retweet: 0,
			comment: 0
		};
		var length = $(this).children().length;
		if (length === 3) {
			tweet.content += $(this).children().first().text();
			var img = $(this).children().eq(1).find("img");
			if (img != null) {
				tweet.content += " " + img.attr("src") + " ";
			}
			$(this).children().eq(2).children().each(function(ii, _elem) {
				var t = $(this).text();
				if (t.indexOf("赞[") !== -1) {
					tweet.fav = t.slice(2, -1);
				} else if (t.indexOf("转发[") !== -1) {
					tweet.retweet = t.slice(3, -1);
				} else if (t.indexOf("评论[") !== -1) {
					tweet.comment = t.slice(3, -1);
				} else if (t === "收藏") {
					
				} else if ($(this).hasClass("ct")) {
					var index = t.indexOf("来自");
					tweet.date = t.slice(0, index - 1);
				} else {
					tweet.content += t;
				}
			});
		} {
			tweet.content += $(this).find(".ctt").text();
			var img = $(this).find("img[alt!='V']");
			if (img.length != 0) {
				tweet.content += " " + img.attr("src") + " ";
			}
			$(this).find("a").each(function(_i, _e) {
				var t = $(this).text();
				if (t.indexOf("赞[") !== -1) {
					tweet.fav = t.slice(2, -1);
				} else if (t.indexOf("转发[") !== -1) {
					tweet.retweet = t.slice(3, -1);
				} else if (t.indexOf("评论[") !== -1) {
					tweet.comment = t.slice(3, -1);
				}
			});
			var date = $(this).find("span.ct").text();
			var index = date.indexOf("来自");
			tweet.date = date.slice(0, index - 1);
		}
		file.write(tweet.date + "," + tweet.fav + "," + tweet.retweet + "," + tweet.comment + "," + tweet.content + ",\n");
	});
	setTimeout(function() {
		main(global_index + 1);
	}, 3000);
}

main(1);